package cn.xgj.sfzy.controller;

import cn.xgj.sfzy.entity.Reply;
import cn.xgj.sfzy.service.ReplyService;
import cn.xgj.sfzy.util.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName ReplyController
 * @Description TODO
 * @Author xgj
 * @Date 2024年10月24日 上午11:03
 * @Version 1.0
 */
@RestController
@RequestMapping("/reply")
public class ReplyController {
    @Autowired
    private ReplyService replyService;

    @RequestMapping("/findAll")
    public Result findAll(Reply reply){
        List<Reply> list = replyService.findAll(reply);
        return Result.success(list);
    }
    @RequestMapping("/findByPage")
    public Result findByPage(
            @RequestParam(required = false,defaultValue = "1") Integer pageNum,
            @RequestParam(required = false,defaultValue = "2") Integer pageSize,
            Reply reply){
        PageInfo<Reply> byPage = replyService.findByPage(pageNum, pageSize, reply);
        return Result.success(byPage);
    }

    @PostMapping
    public Result add(@RequestBody Reply reply){
        int num = replyService.add(reply);
        if(num!=0){
            return Result.success("添加成功");
        }else{
            return Result.error("添加失败");
        }
    }

    @PutMapping
    public Result update(@RequestBody Reply reply){
        int num = replyService.edit(reply);
        if(num!=0){
            return Result.success("修改成功");
        }else{
            return Result.error("修改失败");
        }
    }

    @DeleteMapping("/{replyId}")
    public Result delete(@PathVariable(value = "replyId") Integer replyId){
        int num = replyService.remove(replyId);
        if(num!=0){
            return Result.success("删除成功");
        }else{
            return Result.error("删除失败");
        }
    }
    
    @GetMapping("/{questionId}")
    public Result findById(@PathVariable(value = "questionId") Integer questionId){
        List <Reply> reply = replyService.findByQuestionId(questionId);
        return Result.success(reply);
    }
}
